import java.util.ArrayList;
import java.util.List;


public class num501 {
    //二叉搜索树的众数
    private List<Integer> list = new ArrayList<>();
    int count,maxCount,temp;
    public int[] findMode(TreeNode root) {
        inorder(root);//让list数组存储所有众数
        int[] nums = new int[list.size()];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = list.get(i);
        }
        return nums;
    }

    private void inorder(TreeNode root) {
        if(root == null){
            return;
        }
        inorder(root.left);
        updata(root.val);
        inorder(root.right);
    }
    private void updata(int val){
        if(val == temp){
            count++;
        }else{
            count = 1;
            temp = val;
        }
        if(count == maxCount){
            list.add(val);
        }
        if(count > maxCount){
            list.clear();
            list.add(val);
            maxCount = count;
        }
    }
}
